<template>
  <div class="app-container">
    <RoomTree v-model="currentTree" @change="treeClick" />
    <div class="container_body">
      <el-row :gutter="10" class="mb8">
        <el-button type="primary" icon="el-icon-refresh-left" size="mini" @click="getList">刷新</el-button>
        <el-button
          type="warning"
          icon="el-icon-odometer"
          size="mini"
          :disabled="multiple"
          @click="zhaoce"
          v-hasPermi="['data:htwg:call']"
        >召测</el-button>
        <el-button
          type="success"
          icon="el-icon-odometer"
          size="mini"
          :disabled="multiple"
          @click="clearWg"
          v-hasPermi="['data:htwg:clear']"
        >清除违规</el-button>
      </el-row>

      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
          <el-form-item label="房间号码" prop="roomId">
            <RoomSelect v-model="queryParams.roomId" :params="roomSelectParams" />
          </el-form-item>
          <el-form-item label="通讯状态" prop="runStatus">
            <el-select
              v-model="queryParams.runStatus"
              placeholder="通讯状态"
              clearable
              style="width: 240px"
            >
              <el-option label="全部" value=""></el-option>
              <el-option
                v-for="dict in dict.type.run_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="违规原因" prop="wgReason">
            <el-select
              v-model="queryParams.wgReason"
              placeholder="违规原因"
              clearable
              style="width: 240px"
            >
              <el-option label="全部" value=""></el-option>
              <el-option
                v-for="dict in dict.type.wg_reason"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="htwgList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="房间号码" align="center" prop="roomNo" />
        <el-table-column label="通讯状态" align="center" prop="runStatus">
          <template slot-scope="scope">
            <dict-tag
              :value="scope.row.runStatus"
              :options="dict.type.run_status"
            />
          </template>
        </el-table-column>
        <el-table-column label="违规次数" align="center" prop="wgCount" />
        <el-table-column label="更新时间" align="center" prop="dt" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              icon="el-icon-search"
              @click="handleReason(scope.row)"
            >违规原因</el-button>
          </template>
        </el-table-column>    
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog title="查看违规记录明细" :visible.sync="open" width="500px" append-to-body>
      <el-table v-loading="loading" :data="htwgList">
        <el-table-column label="房间号码" align="center" prop="roomNo" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>

    <ResultStatuListModal ref="ResultStatuListModal" @refresh="getList" />
  </div>
</template>

<script>
import {
  listHtwg, callHtwg, clearHtwg
} from "@/api/deviceMonitor/breakRule";
import RoomSelect from "@/components/RoomSelect";
import RoomTree from "@/components/RoomTree";
import FusionSelect from "@/components/FusionSelect";
export default {
  name: "Room",
  dicts: ["run_status", "wg_reason"],
  components: {
    RoomTree,
    FusionSelect,
    RoomSelect
  },
  data() {
    return {
      zhaoceList: [],
      // 当前选中层级
      currentTree: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      roomSelectParams: {},
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间档案表格数据
      htwgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        runStatus: null,
        wgReason: null
      },
      getListParams: {},
      selection: [],
      meterNos: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 违规原因
    handleReason(row) {},
    /** 查询房间档案列表 */
    getList() {
      this.loading = true;
      listHtwg({...this.queryParams,...this.getListParams}).then(response => {
        this.htwgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId);
      this.selection = selection
      this.meterNos = selection.map(item => item.meterNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    zhaoce() {
      this.$refs.ResultStatuListModal.show({
        title: "您确定要召测数据？",
        actionName: '召测',
        total: this.selection.length,
        handler: (onItemOver) => callHtwg({ rooms: this.selection, meterNos: this.meterNos, onItemOver })
      });
    },
    clearWg() {
      this.$confirm(`您确定要清除违规？`, "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        clearHtwg({ rooms: this.selection, meterNos: this.meterNos })
          .then(res => {
            this.$message.success("操作成功");
            this.getList()
          })
          .finally(() => {
            this.loading = false;
          });
      });
    },
    treeClick() {
      const nameMap = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.roomSelectParams = {
        [nameMap[this.currentTree.name]]: this.currentTree.id
      };
      const nameMap2 = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.getListParams = {
        [nameMap2[this.currentTree.name]]: this.currentTree.id
          // this.currentTree.name == "floor"
          //   ? this.currentTree.data.name.substring(0, 1)
          //   : this.currentTree.id
      };
      this.handleQuery();
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  overflow: hidden;
  .container_body {
    box-sizing: content-box;
    flex: 1;
    max-height: 100%;
    overflow: auto;
    padding-left: 10px;
    padding-right: 5px;
  }
}
</style>
